﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    /// <summary>
    /// 商品记录表业务逻辑层--实现
    /// </summary>
    public class ProductRecordBLL : IProductRecordBLL
    {
        #region 构造函数
        private IProductRecordDAL _productRecordDAL;
        private UnmanContexts _dbContext;
        public ProductRecordBLL(
            IProductRecordDAL productRecordDAL
            , UnmanContexts dbContext
            )
        {
            _productRecordDAL = productRecordDAL;
            _dbContext = dbContext;
        }

        #endregion

        #region 查询商品记录列表的方法--分页（GetProductRecord）
        /// <summary>
        /// 查询商品记录列表的接口--分页
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页几条数据</param>
        /// <param name="consumableId">商品记录Id</param>
        /// <param name="consumableName">商品名</param>
        /// <param name="count">数据总量</param>
        /// <returns></returns>
        public List<GetProductRecordDTO> GetProductRecord(int page, int limit, string consumableId, string consumableName, out int count)
        {
            //linq方法进行链表
            var data = from p in _dbContext.ProductRecord.Where(p => p.IsDalete == false)
                       join c in _dbContext.CommodityInfo.Where(c => c.IsDalete == false)
                       on p.ConsumableId equals c.Id
                       into tempPC
                       from pc in tempPC.DefaultIfEmpty()
                       select new GetProductRecordDTO
                       {
                           Id = p.Id,
                           Creator = p.Creator,
                           Num = p.Num,
                           Type = p.Type,
                           Unit = p.Unit,
                           CreateTime = p.CreateTime,

                           ConsumableId = p.ConsumableId,
                           ConsumableName = pc.CommodityName
                       };
            //商品ID不为空
            if (!string.IsNullOrWhiteSpace(consumableId))
            {
                data = data.Where(p => p.ConsumableId.Contains(consumableId));
            }
            //商品名称
            if (!string.IsNullOrWhiteSpace(consumableName))
            {
                data = data.Where(p => p.ConsumableName.Contains(consumableName));
            }
            //求总数
            count = data.Count();
            //分页
            var listPage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
            //返回结果
            return listPage;
        }

        #endregion

        #region 添加商品记录的方法（CreateProductRecord）
        /// <summary>
        /// 添加商品记录的接口
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CreateProductRecord(ProductRecord entity, out string msg)
        {
            //判断非空信息
            if (string.IsNullOrWhiteSpace(entity.ConsumableId))
            {
                msg = "商品ID不能为空！";
                return false;
            }
            
            if (string.IsNullOrWhiteSpace(entity.Creator))
            {
                msg = "添加人不能为空！";
                return false;
            }

            //判断账号是否重复
            ProductRecord pro = _productRecordDAL.GetEntities().FirstOrDefault(s => s.ConsumableId == entity.ConsumableId);
            if (pro != null)
            {
                msg = "商品ID已经存在！";
                return false;
            }
            //赋值Id
            entity.Id = Guid.NewGuid().ToString();
            //时间
            entity.CreateTime = DateTime.Now;

            //更新到数据库
            bool isSuccess = _productRecordDAL.CreateEntity(entity);

            //新判断
            msg = isSuccess ? $"添加成功！" : "添加失败";
            return isSuccess;
        }
        #endregion

        #region 更新商品记录信息的方法（UpdateProductRecord）
        /// <summary>
        /// 更新商品记录信息的接口
        /// </summary>
        /// <param name="productRecord">要更新的实体</param>
        /// <param name="msg">返回的描述信息</param>
        /// <returns></returns>
        public bool UpdateProductRecord(ProductRecord productRecord, out string msg)
        {
            #region 非空判断
            //非空判断
            if (string.IsNullOrWhiteSpace(productRecord.Id))
            {
                msg = "Id不能为空！";
                return false;
            }
            if (string.IsNullOrWhiteSpace(productRecord.ConsumableId))
            {
                msg = "商品Id不能为空！";
                return false;
            }
            if (string.IsNullOrWhiteSpace(productRecord.Creator))
            {
                msg = "添加人不能为空！";
                return false;
            }

            #endregion

            //判断这个id是否存在商品记录
            ProductRecord entity = _productRecordDAL.GetEntityById(productRecord.Id);
            if (entity == null)
            {
                msg = "id无效";
                return false;
            }
            if (entity.ConsumableId != productRecord.ConsumableId)
            {
                //通过账号查询数据库里是否存在相同的账号
                ProductRecord oldPro = _productRecordDAL.GetEntities().FirstOrDefault(p => p.ConsumableId == productRecord.ConsumableId);
                if (oldPro != null)
                {
                    msg = "已被使用";
                    return false;
                }
            }

            //赋值
            entity.Creator = productRecord.Creator;
            entity.Num = productRecord.Num;
            entity.Type = productRecord.Type;
            entity.Unit = productRecord.Unit;

            //更新到数据库
            bool isOk = _productRecordDAL.UpdateEntity(entity);
            msg = isOk ? "修改成功" : "修改失败";
            //返回结果
            return isOk;
        }
        #endregion

        #region 商品记录软删除的方法（DeleteProductRecord）
        /// <summary>
        /// 商品记录软删除的接口
        /// </summary>
        /// <param name="id">被删除的商品记录id</param>
        /// <returns></returns>
        public bool DeleteProductRecord(string id)
        {
            //根据id查商品记录是否存在
            ProductRecord productRecord = _productRecordDAL.GetEntityById(id);
            if (productRecord == null)
            {
                return false;
            }
            //修改状态
            productRecord.IsDalete = true;
            productRecord.DeleteTime = DateTime.Now;
            //返回结果
            return _productRecordDAL.UpdateEntity(productRecord);
        }
        #endregion

        #region 商品记录批量软删除的方法（DeleteProductRecords）
        /// <summary>
        /// 商品记录批量软删除的接口
        /// </summary>
        /// <param name="ids">被删除的商品记录id集合</param>
        /// <returns></returns>
        public bool DeleteProductRecords(List<string> ids)
        {
            foreach (var id in ids)
            {
                //根据id查商品记录是否存在
                ProductRecord productRecord = _productRecordDAL.GetEntityById(id);
                if (productRecord == null)
                {
                    continue;//跳出本次循环
                }
                //修改状态
                productRecord.IsDalete = true;
                productRecord.DeleteTime = DateTime.Now;
                //执行修改
                _productRecordDAL.UpdateEntity(productRecord);
            }
            return true;
        }
        #endregion

    }
}
